<?php

namespace app\common\model\Order;

use think\Model;

//软删除模型
use traits\model\SoftDelete;

//订单模型
class Order extends Model
{
    //继承软删除
    use SoftDelete;

    //订单表
    protected $table = "pre_order";

    //开启自动写入
    protected $autoWriteTimestamp = true;

    //设置字段的名字
    protected $createTime = 'createtime'; //插入的时候设置的字段名

    //禁止 写入的时间字段
    protected $updateTime = false;

    // 软删除的字段
    protected $deleteTime = 'deletetime';

    protected $append = [
        'status_text',
    ];

    //订单状态获取器
    protected function getStatusTextAttr($value, $data)
    {
        $status = $data['status'];
        $text = '';

        switch ($status) {
            case 1:
                $text = '已支付';
                break;
            case 2:
                $text = '已发货';
                break;
            case 3:
                $text = '已收货';
                break;
            case 4:
                $text = '已评价';
                break;
            case -1:
                $text = "已退货";
                break;
            default:
                $text = "未知状态";
        }
        return $text;
    }

    //查询快递公司
    public function express()
    {
        return $this->belongsTo('app\common\model\Express', 'expid', 'id', [], "LEFT")->setEagerlyType(0);
    }
}
